import React, { Component } from 'react';
import {
    StyleSheet,
    Text,
    View,
    Image,
    TextInput,
    TouchableOpacity,
    DeviceEventEmitter,
    TouchableWithoutFeedback,
    Modal,
    ScrollView
} from 'react-native';

import Constant from '../Constant/Constant'
import ScreenUtils from '../Utils/ScreenUtils'
import ColorUtils from '../Utils/ColorUtils'
import ToastUtils from '../Utils/ToastUtils'
import HttpUtils from '../Utils/HttpUtils'
import RuleUtils from '../Utils/RuleUtils'
import EncryptUtils from '../Utils/EncryptUtils'
import RouterUtils from '../Utils/RouterUtils'
import StorageUtils from '../Utils/StorageUtils'
import TimeUtils from '../Utils/TimeUtils'

export default class UserClock extends React.Component
{
    static navigationOptions = ({navigation}) => ({
        gesturesEnabled: false,//是否支持滑动返回收拾，iOS默认支持，安卓默认关闭
        headerTitle: '我要打卡',
        headerStyle: {
            backgroundColor: ColorUtils.blue,
            height: ScreenUtils.statusBarHeight,
        },
        headerRight: (<Text></Text>),
        headerTintColor: ColorUtils.white,
        headerTitleStyle: {
            fontSize: ScreenUtils.Font_18,
            color: ColorUtils.white,
            fontWeight: 'normal',
            flex: 1,
            alignSelf: 'center',
            textAlign: 'center',
        }
    });

    constructor(props)
    {
        super(props);
        this.state = {
            geoLocation: '',
            geoAddress: '请选择打卡位置',
        };
    }

    componentDidMount()
    {
        RouterUtils.pushRouter('UserClock', this.props.navigation.state.key);
        this.listener1 = DeviceEventEmitter.addListener('postLocation', (events) => this.postLocation(events));
    }

    componentWillUnmount()
    {
        RouterUtils.popRouter('UserClock', this.props.navigation.state.key);
        this.listener1.remove();
    }

    postLocation(event)
    {
        var data = JSON.parse(event);
        this.setState({
            geoAddress: data.address,
            geoLocation: data.geolocation.lng + ',' + data.geolocation.lat
        })
    }

    showBottomItem()
    {
        this.SimpleItemsDialog.show();
    }

    handle()
    {
        try
        {
            var that = this;
            // this.state.geoLocation = "108.870581,34.208749";
            // this.state.geoAddress = '科技七路';
            RuleUtils.checkLocation(this.state.geoAddress);
            var data = {"geo": this.state.geoLocation};
            StorageUtils.get('session').then((session) =>
            {
                if (session != '' && session != null)
                {
                    HttpUtils.postJsonWithRaw(Constant.saveUserClock, session, JSON.stringify(data), function (resultcode, data)
                    {
                        ToastUtils.toastShortBottom('打卡成功')
                        that.props.navigation.goBack();
                    }, function (resultcode, reason)
                    {
                        ToastUtils.toastShortBottom('错误码:' + resultcode + ',错误原因:' + reason);
                    });
                }
            });
        }
        catch (error)
        {
            ToastUtils.toastShortBottom(error);
        }
    }

    jumpLocation()
    {
        this.props.navigation.navigate('WebPage', {'title': '当前位置', 'url': Constant.mapServer +'?type=dingwei'})
    }

    render()
    {
        return (
            <View style={styles.container}>
                <View style={{justifyContent: 'center', alignItems: 'center', paddingLeft: 16, paddingRight: 16}}>
                    <TouchableOpacity activeOpacity={0.8} onPress={() => this.jumpLocation()} style={{flexDirection: 'row', alignItems: 'center', height: 50, paddingTop: 8, paddingBottom: 8, borderBottomWidth: 1, borderBottomColor: ColorUtils.line}}>
                        <Text style={{flex: 1, fontSize: ScreenUtils.Font_14, color: ColorUtils.black}}>打卡地点</Text>
                        <Text numberOfLines={2} style={{textAlign: 'right', width: 220, marginRight: 16, fontSize: ScreenUtils.Font_14, color: ColorUtils.black}}>{this.state.geoAddress}</Text>
                        <View style={{width: 14, justifyContent: 'center', alignItems: 'center'}}>
                            <Image style={{width: 14, height: 18}} source={require('../img/map_location.png')}/>
                        </View>
                    </TouchableOpacity>
                </View>

                <View style={{margin: 32}}>
                    <TouchableOpacity activeOpacity={0.8} onPress={() => this.handle()}>
                        <View style={{justifyContent: 'center', alignItems: 'center', height: 44, padding: 4, borderRadius: 6, backgroundColor: ColorUtils.blue}}>
                            <Text style={{color: ColorUtils.white, fontSize: ScreenUtils.Font_14}}>
                                打卡
                            </Text>
                        </View>
                    </TouchableOpacity>
                </View>
            </View>
        );
    }

}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: ColorUtils.white,
    },
    tabBarUnderlineStyle: {
        height: 2,
        minWidth: Math.floor(ScreenUtils.width * 0.8 / 8),
        backgroundColor: ColorUtils.blue,
    },
    tabBarTextStyle: {
        fontSize: ScreenUtils.Font_14,
        fontWeight: 'normal'
    }
});


